@extends('layouts.admin_common') @section('content')

    @if(app('env')=='production')
        <script src="{{ secure_asset('/js/sy_common.js') }}"></script>
    @else
        <script src="{{ asset('/js/sy_common.js') }}"></script>
    @endif
    <div class="breadcrumbs_container">{{
	Breadcrumbs::render('admin.arrival') }}</div>
    <div class="system_tab_line"></div>

    <div class="lyx_search_table">

        <div class="row">
            <div class="form-group row col-sm-4">
                <label for="inputPassword" class="col-sm-4 col-form-label">@lang('symessages.info.select_order')</label>
                <div class="col-sm-8">
                    <select class="form-control" id="lyx_order_select">
                        <option value="">select purchase order</option>
                        @foreach($orderList as $item)
                            <option value="{{$item->order_id}}">{{$item->order_id.'【'.$item->create_time.'】'}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>

        <div class="lyx_table">

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th scope="col">発注No.</th>
                    <th scope="col">カテゴリー</th>
                    <th scope="col">商品コード</th>
                    <th scope="col">商品名</th>
                    <th scope="col">現在庫</th>
                    <th scope="col">予約数</th>
                    <th scope="col">発注日</th>
                    <th scope="col">発注数</th>
                    <th scope="col">@lang('symessages.info.size')</th>
                    <th scope="col">入荷数</th>
                    <th scope="col">入荷後在庫</th>
                </tr>
                </thead>
                <tbody id="lyx_tbody">
                <tr>
                    <td colspan="11" style="text-align: center">please select suppiler order</td>
                </tr>
                </tbody>
            </table>

        </div>
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-3">
                {{--<button class="btn btn-primary button_normal">Csv取込</button>--}}
            </div>
            <div class="col-sm-3">
                <button class="btn btn-primary button_normal" id="lyx_post_submit">确定</button>
            </div>
            <div class="col-sm-3">
                {{--<button class="btn btn-primary button_normal">WEB反映</button>--}}
            </div>
        </div>
    </div>

    </div>

<script>

    $('#lyx_post_submit').on('click',function () {
        postData();
    })

    order_select_action();

    function order_select_action() {
        $("#lyx_order_select").off().change(function () {
            var value = $(this).val();
            var that = $('#lyx_tbody');
            if (value == null || value == '' || value == undefined) {
                that.html('<tr><td colspan="11" style="text-align: center">empty</td></tr>');
            } else {
                that.html('<tr><td colspan="11" style="text-align: center">loading</td></tr>');
                get_suppiler_order_list(value);
            }
        });
    }

    /**
     * 根据商户订单号获取商户的列表数据
     * @param order_id
     */
    function get_suppiler_order_list(order_id) {
        $.ajax({
            url:'get_suppiler_order_list',
            data:{order_id:order_id},
            success:function (retData) {
                var that = $('#lyx_tbody');
                if(retData.code == 1001){
                    if(retData.data.length > 0){
                        var html = '';
                        $.each(retData.data,function (index,item) {
                            html = html+html_row(item);
                        })
                        that.html(html);
                        num_action();
                    }else{
                        that.html('<tr><td colspan="10" style="text-align: center">empty data !</td></tr>');
                    }
                }else{
                    that.html('<tr><td colspan="10" style="text-align: center">'+retData.msg+'</td></tr>');
                }
            },
            error:function (retData) {
                var msg = [];
                $.each(retData.responseJSON.errors,function (index,item) {
                    msg.push(item[0]);
                })
                if(!msg[0]){
                    msg[0] = retData.responseJSON.message;
                }
                layuiTopMsg(msg[0]);
            }
        })
    }
    
    
    function html_row(data) {
        var html = ['<tr data-info=\''+JSON.stringify(data)+'\'>',
            '                    <td>'+data.order_id+'</td>',
            '                    <td>'+data.category_name+'</td>',
            '                    <td>'+data.goods_code+'</td>',
            '                    <td>'+data.goods_name+'</td>',
            '                    <td>'+data.total_num+'</td>',
            '                    <td>'+data.subscribe+'</td>',
            '                    <td>8/15</td>',
            '                    <td>'+data.num+'</td>',
            '                    <td>'+data.color+'/'+data.size+'</td>',
            '                    <td width="100px"><input class="form-control form-control-sm lyx_house_num" type="number" placeholder=""></td>',
            '                    <td class="last_num"></td>',
            '                </tr>'].join("");
        return html;
    }
    /**
     * 输入数字的事件
     */
    function num_action() {
        $(".lyx_house_num").off().change(function () {
            var value = $(this).val();
            var that = $(this).parents('tr');
            in_house_num_action(value,that);
        })
    }
    /**
     *
     * @param value 填写的数值
     * @param that tr的Dom
     */
    function in_house_num_action(value,that) {
        var info = that.data('info');
        that.data('num',value);
        that.find('.last_num').html(parseInt(info.total_num)+parseInt(value));

    }

    function postData() {
        var goodsList = [];
        $('#lyx_tbody tr').each(function () {
           var info = $(this).data('info');
           var num = $(this).data('num');
           var json = {order_id:info.order_id,goods_id:info.goods_id,num:num,model_info:{goods_model_id:info.model_id,size_name:info.size,color_name:info.color}};
           goodsList.push(json);
        })

        hdAjax({
            url:'suppiler_goods_in_house',
            data:{goodsList:goodsList},
            type:'post',
            success:function (retData) {
                if(retData.code == 1001){
                    successAlert();
                }else{
                    layuiTopMsg(retData.msg);
                }
            }
        })


    }

    
</script>
@endsection

